﻿
namespace MyFurion.Application
{
    /// <summary>
    /// 字典值仓储
    /// </summary>
    public class DictDetailRepository : BaseRepository<DictDetailInfo>,ITransient
    {
        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private Expression<Func<DictDetailInfo, bool>> GetCodition(DictDetailInfoPage input)
        {
            var exp = Expressionable.Create<DictDetailInfo>();
            exp.AndIF(!string.IsNullOrWhiteSpace(input.DictCode), it => it.DictData.DictCode.Contains(input.DictCode));
            exp.AndIF(input.DictId.HasValue, it => it.DictId == input.DictId.Value);
            exp.AndIF(!string.IsNullOrWhiteSpace(input.DictItemCode), it => it.DictItemCode.Contains(input.DictItemCode));
            exp.AndIF(!string.IsNullOrWhiteSpace(input.DictItemName), it => it.DictItemName.Contains(input.DictItemName));      
            exp.AndIF(input.Status.HasValue, it => it.Status == input.Status.Value);
            return exp.ToExpression();
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<DictDetailInfo>> GetPageList(DictDetailInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await GetPageListAsync(whereExp, input.PageIndex, input.PageSize, it => it.SortNum, OrderByType.Asc);
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<DictDetailInfo>> GetDataList(DictDetailInfoPage input)
        {
            var whereExp = GetCodition(input);
            return await GetList(whereExp, it => it.SortNum, OrderByType.Asc);
        }
        /// <summary>
        /// 根据字典code获取字典值信息
        /// </summary>
        /// <param name="dictCode"></param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public async Task<List<DictDetailInfo>> GetByDictCode(string dictCode, EnumStatus? status = null)
        {
            return await Context.Queryable<DictDetailInfo>()
                .Where(it =>it.DictData.DictCode == dictCode)
                .WhereIF(status.HasValue, it => it.Status == status.Value)
                .OrderBy(it => it.SortNum)
                .ToListAsync();
        }
        /// <summary>
        /// 数据是否存在
        /// </summary>
        /// <param name="dictId"></param>
        /// <param name="dictItemCode"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DataExists(long dictId,string dictItemCode, long? id = null)
        {
            return await Context.Queryable<DictDetailInfo>()
                .Where(it => it.DictItemCode==dictItemCode&&it.DictId== dictId)
                .WhereIF(id.HasValue, it => it.Id != id.Value)
                .AnyAsync();
        }
    }
}
